// 历史路由
import {
  createRouter,
  createWebHistory,
  type RouteRecordRaw,
} from "vue-router";
import HomeView from "@/views/home/index.vue";
import { useViewStore } from "@/store/view-store";

export const routes: Array<RouteRecordRaw> = [
  /**
   * 两种加载路由的方式:
   * 1. 静态导入：高频访问页面使用
    // {
    //   path: "/",
    //   name: "Home",
    //   component: HomeView,
    // },
   * 2. 动态导入：按需加载，减少首屏加载时间，低频访问页面使用
    // {
    //   // 动态导入
    //   path: "/",
    //   name: "Home",
    //   component: () => import("@/views/home/index.vue"),
    // },
   */
  {
    path: "/",
    name: "SystemRelated",
    redirect: "/home",
    /**
     * meta 是一个对象，用于存储路由的元信息
     * title: 路由的标题，用于显示在菜单中
     * icon: 路由的图标，用于显示在菜单中
     * showInMenu: 是否显示在菜单中，默认为 true
     * order: 路由的排序，默认为 0
     */
    meta: {
      title: "系统相关",
      icon: "Grid",
      // TODO: showInMenu和order暂时无法发挥作用, 后续再升级
      showInMenu: true,
      order: 0,
    },
    children: [
      {
        path: "/home",
        name: "Home",
        component: HomeView,
        meta: {
          title: "首页",
          icon: "HomeFilled",
        },
        // 判断是否需要进入欢迎页
        beforeEnter: (to, from, next) => {
          const viewStore = useViewStore();
          if (viewStore.isWelcome == true) {
            next("/welcome");
          } else {
            next();
          }
        },
      },
      {
        path: "/welcome",
        name: "Welcome",
        component: () => import("@views/home/welcome.vue"),
        meta: {
          title: "欢迎使用",
          icon: "Goblet",
        },
      },
    ],
  },
  {
    path: "/setting",
    name: "Setting",
    component: () => import("@views/others/setting.vue"),
  },
  {
    path: "/about",
    name: "About",
    component: () => import("@views/others/about.vue"),
  },
  {
    path: "/help",
    name: "Help",
    component: () => import("@views/others/help.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@views/login-related/login.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@views/others/notFound.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 添加路由守卫
// router.beforeEach((to, from, next) => {
//   const viewStore = useViewStore();
//   // 如果是访问欢迎页或首页，直接放行
//   if (to.path === "/welcome" || to.path === "/home") {
//     next();
//     return;
//   }
//   // 否则根据条件重定向
//   if (viewStore.isWelcome) {
//     next("/welcome");
//   } else {
//     next("/home");
//   }
// });

export default router;
